package org.client;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.common.dispatcher.CMD;
import org.common.handler.BaseProtoHandler;
import org.protobuf.AccountMsg;
import org.protobuf.ProtoEnumMsg;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ClientProtoHandler extends BaseProtoHandler {

    PlayerContext playerContext;

    @CMD(ProtoEnumMsg.CMD.ID.ACCOUNT_REGISTER_VALUE)
    public void accountRegisterCallback(Channel channel, AccountMsg.S2CAccountRegister down) throws InvalidProtocolBufferException {
        log.info(JsonFormat.printer().print(down));
    }
    @CMD(ProtoEnumMsg.CMD.ID.ACCOUNT_LOGIN_VALUE)
    public void accountLoginCallback(Channel channel, AccountMsg.S2CAccountLogin down) throws InvalidProtocolBufferException {
        log.info(JsonFormat.printer().print(down));
        if (!down.getSuccess()) {
            return;
        }
        playerContext = new PlayerContext(down.getAccountId());
        List<AccountMsg.SimpleRole> rolesList = down.getRolesList();
        playerContext.setRolesList(rolesList);
    }

    @CMD(ProtoEnumMsg.CMD.ID.ROLE_CREATE_VALUE)
    public void roleCreateCallback(Channel channel, AccountMsg.S2CRoleCreate down) throws InvalidProtocolBufferException {
        log.info(JsonFormat.printer().print(down));
        AccountMsg.SimpleRole simpleRole = down.getSimpleRole();
        playerContext.rolesList.add(simpleRole);
    }


    @CMD(ProtoEnumMsg.CMD.ID.ROLE_LOGIN_VALUE)
    public void roleLoginCallback(Channel channel, AccountMsg.S2CRoleLogin down) throws InvalidProtocolBufferException {
        log.info(JsonFormat.printer().print(down));
        if (!down.getSuccess()) {
            log.error("login failed.");
            return;
        }
    }

}
